﻿/**************************************************************************
    Sayfa:  QueryString ile gelen rapor referansına göre filtre controlleri, grid ve diğer gerekli nesneleri oluşturarak önceden hazırlanmış rapor verisini filtre ederek görüntülemeye yarar.

    Tarih           Sürüm       Değişiklik          İşlem
    --------------------------------------------------------------------------
    2013.Kas        1           Mehmet A. ORAKCI  Oluşturuldu.

****************************************** mehmetakiforakci@gmail.com ****/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Script.Serialization;

namespace RaporProj
{
    public partial class ReportViewControl : System.Web.UI.UserControl
    {
        Int32 _reportId
        {
            get
            {
                return Convert.ToInt32(Request.QueryString["rid"] ?? "0");
            }
        }

        String _sessionId
        {
            get
            {
                return String.Format("Report_{0}_", _reportId);
            }
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (!IsPostBack && !ReportGrid.IsCallback)
                Session[_sessionId + "DataSource"] = null;

            if (!ReportGrid.IsCallback)
            {
                XtraReportManager rm = new XtraReportManager();
                rm.GetReport(_reportId);
                if (rm.Report == null)
                    Response.Redirect("?"); // TODO: Burada kullanıcıya hata mesajı verilebilir
                rm.ExtractXtraReport();

                Session[_sessionId + "ReportManager"] = rm;
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "Report.ClassScriptBlock", rm.Report.GetClassJScriptBlock());
                ReportGridExporter.FileName = String.Format("{0} - {1} - {2:dd-MM-yyyy HH-mm}", rm.Report.CategoryName, rm.Report.Name, DateTime.Now);

                FilterArea _fa = new FilterArea(3, rm);
                fieldcontrolArea.Controls.Add(_fa.GetTable());
                Page.ClientScript.RegisterClientScriptBlock(typeof(string), "FilterArea.ValuesScriptBlock", _fa.ValuesScriptBlock);
            }

            ReportGrid.DataSource = Session[_sessionId + "DataSource"];
            ReportGrid.DataBind();

        }

        protected void ReportGrid_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
        {
            if (!String.IsNullOrEmpty(e.Parameters))
            {
                XtraReportManager rm = Session[_sessionId + "ReportManager"] as XtraReportManager;
                JavaScriptSerializer ser = new JavaScriptSerializer();
                var values = (Dictionary<string, object>)ser.Deserialize(e.Parameters, typeof(Dictionary<string, object>));
                rm.SetOrderedOperandValues(values);
                Session[_sessionId + "DataSource"] = rm.GetData();
                ReportGrid.DataSource = Session[_sessionId + "DataSource"];
                ReportGrid.DataBind();
            }
        }

        protected void ReportExportToolBar_ItemClick(object source, DevExpress.Web.ASPxMenu.MenuItemEventArgs e)
        {
            ReportGrid.DataSource = Session[_sessionId + "DataSource"];
            ReportGrid.DataBind();

            if (e.Item.Name == "Xls")
                ReportGridExporter.WriteXlsxToResponse(true);
            else if (e.Item.Name == "Pdf")
                ReportGridExporter.WritePdfToResponse(true);
            else if (e.Item.Name == "Rtf")
                ReportGridExporter.WriteRtfToResponse(true);
            else if (e.Item.Name == "Csv")
                ReportGridExporter.WriteCsvToResponse(true);
            // else i yok
        }
    }
}